package com.zzyy.study.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import java.io.IOException;
import java.util.Objects;

import com.zzyy.study.annotation.DesensitizeAnnotation;
import com.zzyy.study.enums.DesensitizeEnum;
import com.zzyy.study.utils.PrivacyUtil;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/**
 * @auther zzyy
 * @create 2024-01-20 19:06
 */
@NoArgsConstructor
@AllArgsConstructor
public class PrivacySerializer extends JsonSerializer<String> implements ContextualSerializer
{

    // 脱敏类型
    private DesensitizeEnum desensitizeEnum;
    // 前几位不脱敏
    private Integer prefixNoMaskLen;
    // 最后几位不脱敏
    private Integer suffixNoMaskLen;
    // 用什么打码，* # ？
    private String symbol;

    @Override
    public void serialize(final String origin, final JsonGenerator jsonGenerator,
                          final SerializerProvider serializerProvider) throws IOException
    {
        System.out.println(origin);
        if (StringUtils.isNotBlank(origin) && null != desensitizeEnum)
        {
            switch (desensitizeEnum)
            {
                case CUSTOMER, PASSWORD:
                    jsonGenerator.writeString(PrivacyUtil.desValue(origin, prefixNoMaskLen, suffixNoMaskLen, symbol));
                    break;
                case NAME:
                    jsonGenerator.writeString(PrivacyUtil.hideChineseName(origin));
                    break;
                case ID_CARD:
                    jsonGenerator.writeString(PrivacyUtil.hideIDCard(origin));
                    break;
                case PHONE:
                    jsonGenerator.writeString(PrivacyUtil.hidePhone(origin));
                    break;
                case EMAIL:
                    jsonGenerator.writeString(PrivacyUtil.hideEmail(origin));
                    break;
                default:
                    throw new IllegalArgumentException("脱敏枚举定义中没有此值： " + desensitizeEnum);
            }
        }
    }

    @Override
    public JsonSerializer<?> createContextual(final SerializerProvider serializerProvider,
                                              final BeanProperty beanProperty) throws JsonMappingException
    {
        if (beanProperty != null) {
            if (Objects.equals(beanProperty.getType().getRawClass(), String.class))
            {
                DesensitizeAnnotation desensitizeAnnotation = beanProperty.getAnnotation(DesensitizeAnnotation.class);
                if (desensitizeAnnotation == null) {
                    desensitizeAnnotation = beanProperty.getContextAnnotation(DesensitizeAnnotation.class);
                }
                if (desensitizeAnnotation != null) {
                    return new PrivacySerializer(desensitizeAnnotation.type(),
                            desensitizeAnnotation.prefixNoMaskLen(),
                            desensitizeAnnotation.suffixNoMaskLen(),
                            desensitizeAnnotation.symbol());
                }
            }
            return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
        }
        return serializerProvider.findNullValueSerializer(null);
    }
}
